// packageG/offlineHongbao/offlineHongbao.js
var o = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        moneyList: [],
        lingquList: [

        ],
        statusBarHeight: 0,// 状态栏高度
        navBarHeight: 0,// 导航栏高度
        navStatusBarHeight: 0, // 导航栏和状态栏高度
        menu: 0,// 胶囊
        tabslist: [
            { title: '消费金额' },
            { title: '领取记录' }
        ],
        tabsIndex: 0,//0消费金额 1领取记录
        allChecked: false,//全选
        hongbaoShow: false,//领红包弹窗
        amount: 0,//红包总额度
        is_draw: 0,//领取总金额
        itemid: [],
        newmoney: '',
        countPage: '',
        page: 1,
    },
    changeTabs(e) {
        console.log(e.currentTarget.dataset.index, 'ksksksk')
        this.setData({
            tabsIndex: e.currentTarget.dataset.index
        })
        if (e.currentTarget.dataset.index == 1) {
            // 领取记录
            this.getRecordList()
        }
    },
    checkedList(e) {
        console.log(e.currentTarget.dataset.items.id, 'kekekejjj')
        let index = e.currentTarget.dataset.index
        this.data.moneyList[index].checked = !this.data.moneyList[index].checked
        this.setData({
            moneyList: this.data.moneyList
        })
        const hasActiveItem = this.data.moneyList.some(item => item.checked === false);
        console.log(hasActiveItem, 'hasActiveItem'); // 输出: true
        if (!hasActiveItem) {
            this.setData({
                allChecked: true
            })
        } else {
            this.setData({
                allChecked: false
            })
        }
    },
    // 改变全选状态
    changeAllCheck() {
        this.setData({
            allChecked: !this.data.allChecked
        })
        console.log(this.data.moneyList, 'this.data.moneyList1')
        if (this.data.allChecked == true) {
            this.data.moneyList.forEach((item) => {
                if (item.status == 1) {
                    item.checked = true
                } else {
                    item.checked = false
                }

            })
        } else {
            this.data.moneyList.forEach((item) => {
                item.checked = false
            })
        }
        console.log(this.data.moneyList, 'this.data.moneyList')
        this.setData({
            moneyList: this.data.moneyList
        })
    },
    goback() {
        wx.navigateBack({
            delta: 1
        })
    },
    // 领红包
    lingHong(e) {
        let type = e.currentTarget.dataset.type
        let id = e.currentTarget.dataset.id
        var idList = [];
        if (type == 1) {
            idList.push(id)
            this.getXiaofeiMoney()
            console.log(id, '我是单选id')
        } else if (type == 2) {
            let arr = this.data.moneyList
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].checked == true) {
                    idList.push(arr[i].id)
                }
            }
            console.log(idList, 'idList')
            this.getXiaofeiMoney()
        }

        var e = this, t = o.getNetAddresss("FeilongStore.hongbao");
        o._getNetWork({
            url: t,
            data: {
                uid: wx.getStorageSync("yz_uid"),
                log_id: JSON.stringify(idList)
            },
            success: function (a) {
                console.log(a.data.data, '看看是啥')
                e.setData({
                    newmoney: a.data.data
                })
            }
        });
        this.setData({
            hongbaoShow: true
        })
    },
    //关闭领取红包弹窗
    closeHongPop() {
        this.setData({
            hongbaoShow: false
        })
        this.getXiaofeiMoney()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getSystemInfo()
        this.getXiaofeiMoney()
    },
    // 领取记录
    getRecordList() {
        var e = this, t = o.getNetAddresss("FeilongStore.drawList");
        o._getNetWork({
            url: t,
            data: {
                uid: wx.getStorageSync("yz_uid"),
                page: this.data.page
            },
            success: function (a) {
                console.log(a.data.data.data, '看看是啥')
                if (a.data.result == 1) {
                    e.setData({
                        countPage: a.data.data.last_page,
                    })
                    if (e.data.page == 1) {
                        var lingquList = a.data.data.data
                    } else {
                        var lingquList = e.data.lingquList
                        lingquList = lingquList.concat(a.data.data.data)
                    }
                    e.setData({
                        lingquList: lingquList,
                    })
                    console.log('09090', lingquList)
                }
            }
        });
    },
    getXiaofeiMoney() {
        var e = this, t = o.getNetAddresss("FeilongStore.storeHongbao");
        o._getNetWork({
            url: t,
            data: {
                uid: wx.getStorageSync("yz_uid"),
            },
            success: function (a) {
                if (a.data.result == 1) {
                    e.setData({
                        amount: a.data.data.amount,
                        is_draw: a.data.data.is_draw,
                        moneyList: a.data.data.list
                    })
                }
            }
        });
    },
    getSystemInfo() {
        let _this = this;
        // 获取手机系统信息
        wx.getSystemInfo({
            success: res => {
                // 手机系统状态栏高度
                //   wx.setStorageSync('statusBarHeight', res.statusBarHeight)
                _this.setData({
                    statusBarHeight: res.statusBarHeight
                })
                const platform = res.platform
                const menu = wx.getMenuButtonBoundingClientRect()
                //menu为胶囊，判断是否能读到胶囊位置，读不到则用具体一般数值表示
                if (menu) {
                    _this.setData({
                        menu: menu,
                        navBarHeight: menu.height + (menu.top - res.statusBarHeight) * 2,
                        navStatusBarHeight: res.statusBarHeight + menu.height + (menu.top - res.statusBarHeight) * 2
                    })
                    // wx.setStorageSync('menu', menu)
                    _this.menu = menu

                    // 导航栏高度
                    // wx.setStorageSync('navBarHeight', menu.height+(menu.top-res.statusBarHeight) *2 )
                    _this.navBarHeight = menu.height + (menu.top - res.statusBarHeight) * 2
                    // 状态栏加导航栏

                    // wx.setStorageSync('navStatusBarHeight', res.statusBarHeight+ menu.height+(menu.top-res.statusBarHeight) *2 )
                    _this.navStatusBarHeight = res.statusBarHeight + menu.height + (menu.top - res.statusBarHeight) * 2

                } else {
                    _this.setData({
                        menu: null,
                        navBarHeight: platform === 'android' ? 48 : 44,
                        navStatusBarHeight: res.statusBarHeight + (platform === 'android' ? 48 : 44)
                    })
                    // wx.setStorageSync('menu', null)
                    _this.menu = null
                    // 导航栏高度
                    // wx.setStorageSync('navBarHeight', platform === 'android' ? 48 : 44)
                    _this.navBarHeight = platform === 'android' ? 48 : 44
                    // 状态栏加导航栏

                    //  wx.setStorageSync('navStatusBarHeight', res.statusBarHeight+ (platform === 'android' ? 48 : 44) )
                    _this.navStatusBarHeight = res.statusBarHeight + (platform === 'android' ? 48 : 44)
                }
            }, fail(err) {
                console.log(err);
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.data.page = 1;
        this.getRecordList()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('到达底部')
        if (this.data.countPage > this.data.page) {
            this.data.page++;
            this.getRecordList()
        } else {
            if (this.data.page != 1) {
                wx.showToast({
                    title: '已加载全部',
                    icon: 'none'
                })
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})